package com.qef.com.mmovie.fragment.video_jc;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.qef.com.mmovie.App;
import com.qef.com.mmovie.R;
import com.qef.com.mmovie.base.BaseFragment;
import com.qef.com.mmovie.base.BaseRecycerViewAdapter;
import com.qef.com.mmovie.entity.ItemListBean;
import com.qef.com.mmovie.entity.VideoPlayEntity;
import com.qef.com.mmovie.entity.VideoTopEntity;
import com.qef.com.mmovie.mvp.adapter.RecyclerViewAnimator;
import com.qef.com.mmovie.mvp.adapter.VideoAdapter;
import com.qef.com.mmovie.mvp.adapter.VideoTopAdapter;
import com.qef.com.mmovie.presenter.VideoTopHomePresenter;
import com.qef.com.mmovie.presenter.contract.VideoTopHomeContract;
import com.qef.com.mmovie.utils.ToastUtil;
import com.qef.com.mmovie.widget.EndLessOnScrollListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by guangjiego on 2017/3/23.
 */

public class VideoTopHomeFragment extends BaseFragment<VideoTopHomePresenter> implements VideoTopHomeContract.View {
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    VideoTopAdapter adapter;
    private List<VideoTopEntity.DataEntity.ResEntity> mVideoListBean;
    private JCVideoPlayerStandard mPlayer;
    private View mIv;

    public static VideoTopHomeFragment newInstance() {
        Bundle bundle = new Bundle();
        VideoTopHomeFragment videoFragment = new VideoTopHomeFragment();
        videoFragment.setArguments(bundle);
        return videoFragment;
    }


    @Override
    public void showContent(VideoTopEntity videoListBean) {
        if (refresh.isRefreshing()) {
            refresh.setRefreshing(false);
            mVideoListBean.clear();
            adapter.clear();
            adapter.addAll(videoListBean.getData().getRes());
//            recycler.setAdapter(adapter);
        } else {
            adapter.addAll(videoListBean.getData().getRes());
        }
        mVideoListBean.addAll(videoListBean.getData().getRes());

//        int end = videoListBean.getNextPageUrl().lastIndexOf("&num");
//        int start = videoListBean.getNextPageUrl().lastIndexOf("date=");
//        date = videoListBean.getNextPageUrl().substring(start + 5, end);
        adapter.setOnItemClickListener((position, view, vh) -> {
            //获取视频播放数据
            mPlayer = null;
            mPlayer = ((VideoTopAdapter.MyHolder) vh).jcVideoPlayer;
            mPlayer.release();
            mIv = ((VideoTopAdapter.MyHolder) vh).rl_video_info;
            mPresenter.getVideoPlayUrl(adapter.list.get(position).getExData().getCode());
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != mPresenter && null != mPlayer)
            mPlayer.release();
    }

    @Override
    public void error(Throwable throwable) {

    }

    @Override
    public void playVideo(VideoPlayEntity entity) {
        mIv.setVisibility(View.GONE);
        mPlayer.setVisibility(View.VISIBLE);
        mPlayer.setUp(entity.getData().getUrl(), JCVideoPlayer.SCREEN_LAYOUT_LIST, "");
        mPlayer.startButton.performClick();
//        mPlayer.performClick();
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.video_top_home_fragment;
    }

    private int page = 1;

    @Override
    protected void initEventAndData() {
        refresh.setBackgroundColor(getResources().getColor(R.color.page2));
        refresh.setProgressViewOffset(false, 100, 200);
        refresh.setRefreshing(true);
        LinearLayoutManager layout = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        refresh.setOnRefreshListener(() -> {
            mPresenter.getVideoData(page = 1);
        });


        recycler.setLayoutManager(layout);
        mVideoListBean = new ArrayList<>();
        adapter = new VideoTopAdapter(getActivity(), mVideoListBean);
        recycler.setAdapter(RecyclerViewAnimator.setAnimator(adapter));
        recycler.addOnScrollListener(new EndLessOnScrollListener(layout, 0) {
            @Override
            public void onLoadMore() {
                page++;
                mPresenter.getVideoData(page);
            }
        });
        mPresenter.getVideoData(page);
    }

}
